package o;

import com.huawei.appmarket.pkisign.zip.ZipFormatException;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

public class dmg {
    private static final ByteBuffer ˎ = ByteBuffer.allocate(0);
    private final boolean ʻ;
    private final long ʼ;
    private final ByteBuffer ˊ;
    private final long ˋ;
    private final int ˏ;
    private final String ॱ;
    private final long ᐝ;

    static class b implements dme, Closeable {
        private long ˊ;
        private Inflater ˋ;
        private final dme ˎ;
        private byte[] ˏ;
        private boolean ॱ;

        private b(dme o_dme) {
            this.ˋ = new Inflater(true);
            this.ˎ = o_dme;
        }

        public void ˋ(byte[] bArr, int i, int i2) throws IOException {
            ˊ();
            this.ˋ.setInput(bArr, i, i2);
            if (this.ˏ == null) {
                this.ˏ = new byte[WXMediaMessage.THUMB_LENGTH_LIMIT];
            }
            while (!this.ˋ.finished()) {
                try {
                    int inflate = this.ˋ.inflate(this.ˏ);
                    if (inflate != 0) {
                        this.ˎ.ˋ(this.ˏ, 0, inflate);
                        this.ˊ = ((long) inflate) + this.ˊ;
                    } else {
                        return;
                    }
                } catch (Throwable e) {
                    throw new IOException("Failed to inflate data", e);
                }
            }
        }

        private long ˏ() {
            return this.ˊ;
        }

        public void close() {
            this.ॱ = true;
            this.ˏ = null;
            if (this.ˋ != null) {
                this.ˋ.end();
                this.ˋ = null;
            }
        }

        private void ˊ() {
            if (this.ॱ) {
                throw new IllegalStateException("Closed");
            }
        }
    }

    private dmg(String str, ByteBuffer byteBuffer, long j, int i, long j2, boolean z, long j3) {
        this.ॱ = str;
        this.ˊ = byteBuffer;
        this.ˋ = j;
        this.ˏ = i;
        this.ᐝ = j2;
        this.ʻ = z;
        this.ʼ = j3;
    }

    private static dmg ˎ(dmm o_dmm, dmd o_dmd, long j, boolean z) throws ZipFormatException, IOException {
        String ॱ = o_dmd.ॱ();
        int ˊ = o_dmd.ˊ();
        int i = ˊ + 30;
        long ʼ = o_dmd.ʼ();
        long j2 = ((long) i) + ʼ;
        if (j2 > j) {
            throw new ZipFormatException("Local File Header of " + ॱ + " extends beyond start of Central Directory. LFH end: " + j2 + ", CD start: " + j);
        }
        try {
            ByteBuffer ॱ2 = o_dmm.ॱ(ʼ, i);
            ॱ2.order(ByteOrder.LITTLE_ENDIAN);
            i = ॱ2.getInt();
            if (i != 67324752) {
                throw new ZipFormatException("Not a Local File Header record for entry " + ॱ + ". Signature: 0x" + Long.toHexString(((long) i) & 4294967295L));
            }
            boolean z2 = (ॱ2.getShort(6) & 8) != 0;
            boolean z3 = (o_dmd.ˎ() & 8) != 0;
            if (z2 != z3) {
                throw new ZipFormatException("Data Descriptor presence mismatch between Local File Header and Central Directory for entry " + ॱ + ". LFH: " + z2 + ", CD: " + z3);
            }
            ॱ(o_dmd, ॱ2, z3);
            i = dmh.ˎ(ॱ2, 26);
            if (i > ˊ) {
                throw new ZipFormatException("Name mismatch between Local File Header and Central Directory for entry" + ॱ + ". LFH: " + i + " bytes, CD: " + ˊ + " bytes");
            }
            String ˏ = dmd.ˏ(ॱ2, 30, i);
            if (ॱ.equals(ˏ)) {
                long ॱॱ;
                int ˎ = dmh.ˎ(ॱ2, 28);
                long j3 = ((30 + ʼ) + ((long) i)) + ((long) ˎ);
                boolean z4 = o_dmd.ˏ() != (short) 0;
                if (z4) {
                    ॱॱ = o_dmd.ॱॱ();
                } else {
                    ॱॱ = o_dmd.ᐝ();
                }
                long j4 = j3 + ॱॱ;
                if (j4 > j) {
                    throw new ZipFormatException("Local File Header data of " + ॱ + " overlaps with Central Directory. LFH data start: " + j3 + ", LFH data end: " + j4 + ", CD start: " + j);
                }
                ByteBuffer byteBuffer = ˎ;
                if (z && ˎ > 0) {
                    byteBuffer = o_dmm.ॱ((30 + ʼ) + ((long) i), ˎ);
                }
                return new dmg(ॱ, byteBuffer, ʼ, (i + 30) + ˎ, ॱॱ, z4, o_dmd.ᐝ());
            }
            throw new ZipFormatException("Name mismatch between Local File Header and Central Directory. LFH: \"" + ˏ + "\", CD: \"" + ॱ + "\"");
        } catch (Throwable e) {
            throw new IOException("Failed to read Local File Header of " + ॱ, e);
        }
    }

    private static void ॱ(dmd o_dmd, ByteBuffer byteBuffer, boolean z) throws ZipFormatException {
        long ˋ = o_dmd.ˋ();
        long ॱॱ = o_dmd.ॱॱ();
        long ᐝ = o_dmd.ᐝ();
        if (!z) {
            long ˋ2 = dmh.ˋ(byteBuffer, 14);
            if (ˋ2 != ˋ) {
                throw new ZipFormatException("CRC-32 mismatch between Local File Header and Central Directory for entry . LFH: " + ˋ2 + ", CD: " + ˋ);
            }
            ˋ = dmh.ˋ(byteBuffer, 18);
            if (ˋ != ॱॱ) {
                throw new ZipFormatException("Compressed size mismatch between Local File Header and Central Directory for entry . LFH: " + ˋ + ", CD: " + ॱॱ);
            }
            ˋ = dmh.ˋ(byteBuffer, 22);
            if (ˋ != ᐝ) {
                throw new ZipFormatException("Uncompressed size mismatch between Local File Header and Central Directory for entry . LFH: " + ˋ + ", CD: " + ᐝ);
            }
        }
    }

    private static void ˋ(dmm o_dmm, dmd o_dmd, long j, dme o_dme) throws ZipFormatException, IOException {
        ˎ(o_dmm, o_dmd, j, false).ˎ(o_dmm, o_dme);
    }

    public static byte[] ˋ(dmm o_dmm, dmd o_dmd, long j) throws ZipFormatException, IOException {
        if (o_dmd.ᐝ() > 2147483647L) {
            throw new IOException(o_dmd.ॱ() + " too large: " + o_dmd.ᐝ());
        }
        byte[] bArr = new byte[((int) o_dmd.ᐝ())];
        ˋ(o_dmm, o_dmd, j, new dlz(ByteBuffer.wrap(bArr)));
        return bArr;
    }

    private void ˎ(dmm o_dmm, dme o_dme) throws IOException, ZipFormatException {
        long j = ((long) this.ˏ) + this.ˋ;
        try {
            if (this.ʻ) {
                Object bVar = new b(o_dme);
                o_dmm.ˊ(j, this.ᐝ, bVar);
                long ˊ = bVar.ˏ();
                if (ˊ != this.ʼ) {
                    throw new ZipFormatException("Unexpected size of uncompressed data of " + this.ॱ + ". Expected: " + this.ʼ + " bytes, actual: " + ˊ + " bytes");
                }
                return;
            }
            o_dmm.ˊ(j, this.ᐝ, o_dme);
        } catch (IOException e) {
            if (e.getCause() instanceof DataFormatException) {
                throw new ZipFormatException("Data of entry " + this.ॱ + " malformed" + e.getMessage());
            }
            throw e;
        } catch (Throwable e2) {
            String str;
            Throwable th = e2;
            StringBuilder append = new StringBuilder().append("Failed to read data of ");
            if (this.ʻ) {
                str = "compressed";
            } else {
                str = "uncompressed";
            }
            throw new IOException(append.append(str).append(" entry ").append(this.ॱ).toString(), th);
        }
    }
}
